package com.yy.ttmodule.manager;


import android.support.annotation.NonNull;

import com.yy.ttmodule.control.Constant;
import com.yy.ttmodule.control.ParamCons;
import com.yy.ttmodule.udp.DataTypeConversion;
import com.yy.ttmodule.udp.RxDataCallBack;
import com.yy.ttmodule.udp.UdpServer;
import com.yy.ttmodule.utils.Logger;

public class ControlManager {

    private static volatile ControlManager instance;

    UdpServer mUdpServer;

    int SMS_LENTH_320 = 320;
    int SMS_LENTH_960 = 960;
    int NUM_LENTH_11 = 11;
    int SMS_LENTH_70 = 70;

    private ControlManager() {
    }

    public static ControlManager getInstance() {
        if (instance == null) {
            synchronized (ControlManager.class) {
                if (instance == null) {
                    instance = new ControlManager();
                }
            }
        }
        return instance;
    }

    public void init() {
        mUdpServer = new UdpServer(1024 * 1024, Constant.SERVER_PORT);
        mUdpServer.start();
    }

    //入网状态 查询返回或主动上报
    public void ackNetAccessStatus(int status) {
        //new byte[]{0x0, ParamCons.CMD_NETWORK_ACCESS_STATUS_RES}
//        TxData.getInstance().setParam(DataTypeConversion.shortToBytesBig((short)status));
//        TxData.getInstance().setData(DataTypeConversion.intLowToArray(status));
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_NETWORK_ACCESS_STATUS_RES),
                DataTypeConversion.intLowToArray(status));
    }

    //信号强度查询结果
    public void ackSignalStrength(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_SIGNAL_STRENGTH_QUERY_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //数据状态查询结果
    public void ackDataStatus(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_DATA_STATUS_QUERY_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //数据状态设置结果
    public void ackDataStatusSetRes(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_DATA_STATUS_SET_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //飞行模式查询结果
    public void ackFlightMode(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_FLIGHT_MODE_QUERY_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //飞行模式设置结果
    public void ackFlightModeSet(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_FLIGHT_MODE_SET_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //本机号码发送
    public void ackPhoneNum(String number) {
        if ( number.length() > NUM_LENTH_11 ) {
            return;
        }
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_LOCAL_PHONE_NUMBER_RES),
                number.getBytes());
    }

    //密卡状态
    public void ackCardStatus(int mCard, int mStick) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_CARD_STATUS_RES),
                new byte[]{ (byte)(mCard&0x0FF), (byte)(mStick&0x0FF)});
    }

    //拨号结果
    public void ackDialRes(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_DIAL_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //来电
    public void ackIncomingCall(String number) {
        if ( number.length() > NUM_LENTH_11 ) {
            return;
        }
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_INCOMING_CALL),
                number.getBytes());
    }

    //接听结果
    public void ackAnswerRes(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_ANSWER_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //挂断结果
    public void ackHangUpRes(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_HANG_UP_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //通话状态
    public void ackCallStatus(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_CALL_STATUS_INQUIRY_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //下行PCM接收数据
    public void ackDownlinkPcmData(byte[] param) {
        if ( param.length != SMS_LENTH_320 || param.length != SMS_LENTH_960 ) {
            return ;
        }
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_DOWNLINK_PCM_RECEIVE_DATA),
                param);
    }

    //发送短信结果
    public void ackSendSmsRes(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_SEND_TEXT_MESSAGE_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //接收短信
    public void ackReceiveSmsRes(String number, String smsText) {
        try {
            if ( number.length() > NUM_LENTH_11 || smsText.length() > SMS_LENTH_70 ) {
                return;
            }
            byte[] txtArr = smsText.getBytes("GBK");
            byte[] res = new byte[number.getBytes().length + txtArr.length];

            System.arraycopy(number.getBytes(), 0, res, 0, number.getBytes().length);
            System.arraycopy(txtArr, 0, res, number.getBytes().length, txtArr.length);
            mUdpServer.sendPackage(
                    DataTypeConversion.shortToBytes(ParamCons.CMD_RECEIVE_SMS), res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //重启结果
    public void ackRebootRes(int param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_REBOOT_RES),
                DataTypeConversion.intLowToArray(param));
    }

    //北斗定位信息查询结果
    public void ackBeidouNavRes(byte[] param) {
        mUdpServer.sendPackage(
                DataTypeConversion.shortToBytes(ParamCons.CMD_BEIDOU_NAV_QUERY_RES), param);
    }

    public void destory() {
        if ( mUdpServer != null ) {
            mUdpServer.stop();
        }
    }

    public synchronized void regRxDataCallBack(@NonNull RxDataCallBack callBack) {
        mUdpServer.regRxDataCallBack(callBack);
    }

}
